using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace EVEChatLib
{
    public partial class Lib
    {
        // Comments coming soon!
        public void arrangeVertical(Panel thePanel)
        {
            Control[] controller = thePanel.Controls.Find("ChatWindow", false);

            if (controller.Length > 0)
            {
                int activeForm = 0;
                foreach (Control controlForm in thePanel.Controls.Find("ChatWindow", false))
                {
                    Form form = controlForm.FindForm();
                    form.WindowState = FormWindowState.Normal;
                    form.Location = new Point(((thePanel.Width / controller.Length) * activeForm), 0);
                    form.Width = thePanel.Width / controller.Length;
                    form.Height = thePanel.Height;
                    activeForm++;
                }
            }
        }

        public void arrangeHorizontal(Panel thePanel)
        {
            Control[] controller = thePanel.Controls.Find("ChatWindow", false);

            if (controller.Length > 0)
            {
                int activeForm = 0;
                foreach (Control controlForm in thePanel.Controls.Find("ChatWindow", false))
                {
                    Form form = controlForm.FindForm();
                    form.WindowState = FormWindowState.Normal;
                    form.Location = new Point(0, ((thePanel.Height / controller.Length) * activeForm));
                    form.Width = thePanel.Width;
                    form.Height = thePanel.Height / controller.Length;
                    activeForm++;
                }
            }
        }

        public void arrangeSmart(Panel thePanel)
        {
            Control[] controller = thePanel.Controls.Find("ChatWindow", false);

            if (controller.Length > 0)
            {
                if (controller.Length <= 2)
                {
                    this.arrangeVertical(thePanel);
                }
                else
                {
                    int activeForm = 0;
                    int width = 0;
                    int height = 0;
                    int x = 0;
                    int y = 0;
                    bool odd = true;
                    bool top = true;

                    if ((controller.Length % 2) == 0)
                        odd = false;
                    else
                    {
                        odd = true;
                    }


                    foreach (Control controlled in thePanel.Controls.Find("ChatWindow", false))
                    {
                        Form form = controlled.FindForm();
                        if (odd)
                        {
                            width = thePanel.Width / ((controller.Length + 1) / 2);
                            if (activeForm + 1 == (controller.Length))
                                height = thePanel.Height;
                            else
                            {
                                height = thePanel.Height / 2;
                            }
                        }
                        else
                        {
                            width = thePanel.Width / (controller.Length / 2);
                            height = thePanel.Height / 2;
                        }

                        if (top)
                        {
                            x = ((activeForm + 1) / 2) * width;
                            y = 0;
                            top = false;
                        }
                        else
                        {
                            x = (activeForm / 2) * width;
                            y = thePanel.Height / 2;
                            top = true;
                        }

                        form.Location = new Point(x, y);
                        form.Width = width;
                        form.Height = height;
                        activeForm++;
                    }
                }
            }
        }

        public void arrangeCascade(Panel thePanel)
        {
            int activeForm = 0;
            int x = 0;
            int y = 0;

            foreach (Control controlled in thePanel.Controls.Find("ChatWindow", false))
            {
                Form form = controlled.FindForm();

                form.Location = new Point(x, y);
                form.Width = thePanel.Width / 2;
                form.Height = 268;
                activeForm++;

                x += form.Width / 6;
                y += form.Height / 4;
            }
        }

        public void goFullscreen(Form theForm, ToolStrip theToolstrip)
        {

            if (theForm.FormBorderStyle.Equals(FormBorderStyle.None))
            {
                theForm.FormBorderStyle = FormBorderStyle.Sizable;
                theForm.WindowState = FormWindowState.Normal;

                foreach( ToolStripItem tsi in theToolstrip.Items.Find("goFullscreen", false))
                {
                    tsi.Text = "Go Fullscreen";
                    tsi.ToolTipText = "Go Fullscreen";
                    tsi.Image = EVEChatLib.Properties.Resources.fullscreen;
                }
            }
            else
            {
                theForm.FormBorderStyle = FormBorderStyle.None;
                theForm.WindowState = FormWindowState.Maximized;

                foreach (ToolStripItem tsi in theToolstrip.Items.Find("goFullscreen", false))
                {
                    tsi.Text = "Restore";
                    tsi.ToolTipText = "Restore";
                    tsi.Image = EVEChatLib.Properties.Resources.restore;
                }
            }
        }
    }
}
